﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class admin1_Flashbanner : System.Web.UI.Page
{
    eStoreDataContext db = new eStoreDataContext();
    protected void Page_PreRender(object sender, EventArgs e)
    {
        rpt_flash.DataSource = db.Banners;
        rpt_flash.DataBind();

        // lay product Id
        ddlproductId.DataSource = db.Tours;
        ddlproductId.DataValueField = "TourId";
        ddlproductId.DataTextField = "NameVN";
        ddlproductId.DataBind();
    }
    protected void btnxoa_Click(object sender, EventArgs e)
    {
        int id = int.Parse(((LinkButton)sender).CommandArgument.ToString());
        //Response.Write("<script>alert("+id+")</script>");
        //1. tim
        var cate = db.Banners.Single(c => c.BannerId == id);
        //2. Xoa
        db.Banners.DeleteOnSubmit(cate);
        //3. luu
        db.SubmitChanges();
    }
    protected void btnInsert_Click(object sender, EventArgs e)
    {
        try
        {


            var flash = new Banner
            {
                Image = Images2.Text,
                TourId = int.Parse(ddlproductId.SelectedValue),
                Activated = bool.Parse(radEdit.SelectedValue)
            };

            //2.them vao
            db.Banners.InsertOnSubmit(flash);
            //3. luu
            db.SubmitChanges();
            Response.Redirect("Flashbanner.aspx");
        }
        catch (Exception ex)
        {
            Response.Write("<script> alert('Thêm không thành công! ');</script>");

        }
    }
    protected void lbt_ChinhSua_Click(object sender, EventArgs e)
    {
        panel_Edit.Visible = true;
        int bannerid = int.Parse(((LinkButton)sender).CommandArgument.ToString());
        // tim 
        var pla = db.Banners.Single(c => c.BannerId == bannerid);
        // load thong tin
        txt_BannerId_Edit.Text = bannerid.ToString();
        Image1.ImageUrl = pla.Image.ToString();
        ddl_Tour_Edit.DataSource = db.Tours;
        ddl_Tour_Edit.DataValueField = "TourId";
        ddl_Tour_Edit.DataTextField = "NameVN";
        ddl_Tour_Edit.DataBind();

        ddl_Tour_Edit.SelectedValue = pla.TourId.ToString();

        if (pla.Activated == true)
        {
            radEdit.SelectedIndex = 0;
        }
        else
        {
            radEdit.SelectedIndex = 1;
        }
    }
    protected void btn_Save_Edit_Click(object sender, EventArgs e)
    {
        try
        {
            // tim 
            var pla = db.Banners.Single(c => c.BannerId == int.Parse(txt_BannerId_Edit.Text));
            if (Images.Text.Trim() != "")
            {
                pla.Image = Images.Text;
            }
            pla.TourId = int.Parse(ddl_Tour_Edit.SelectedValue);
            pla.Activated = bool.Parse(radEdit.SelectedValue);

            //3 Luu
            db.SubmitChanges();
            panel_Edit.Visible = false;
        }
        catch (Exception ex)
        {
            Response.Write("<script> alert('Chỉnh sửa  không thành công! ');</script>");
        }
        rpt_flash.DataBind();
    }
    protected void lbt_ClosAdd_Click(object sender, EventArgs e)
    {
        panel_Add.Visible = false;
    }
    protected void lbt_ClosEdit_Click(object sender, EventArgs e)
    {
        panel_Edit.Visible = false;
    }
    protected void lbt_Add_New_Click(object sender, EventArgs e)
    {
        panel_Edit.Visible = false;
        panel_Add.Visible = true;
    }

}